/** @file    SocketUdp.cpp
 *  @author  Alessandro Polo
 *  @version $Id: SocketUdp.cpp 2850 2010-08-06 18:49:18Z alex $
 *  @brief
 * File containing methods for the wosh::network::SocketUdp class.
 * The header for this class can be found in SocketUdp.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/network/SocketUdp.h>
 #include <core/ObjectAllocator.h>
 #include <core/Utilities.h>
 #include <framework/network/NetworkUtilities.h>


#ifdef _SocketUdp_DEBUG_ENABLED
# define LOG( LEVEL, FORMAT, ... ) Log(LEVEL, FORMAT, __VA_ARGS__);
#else
# define LOG( LEVEL, FORMAT, ... ) ;
#endif


namespace wosh {
 namespace network {

  WOSH_REGISTER_INTERFACE(wosh::network::SocketUdp, "", 1.00, _static_SocketUdp )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

SocketUdp::SocketUdp() {
	this->listener = NULL;
	this->ignoreSelf = false;

	this->address = "";
	this->port = 0;
	this->address_port = "";
#ifdef _SocketUdp_DEBUG_ENABLED
	Log.setContext( "SocketUdp" );
	Log.setLevel( LOG_INFO );
	Log.setIndent( 5 );
#endif
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT SocketUdp::bindSocket( const std::string& address_port, unsigned long timeout ) {
	if ( !setAddressPort(address_port) ) {
		LOG(LOG_CRITICAL, ":bindSocket(%s, %lu) FAILED Setting address", address_port.c_str(), timeout );
		return WRET_ERR_PARAM;
	 }
	return bindSocket(timeout);
 }
WRESULT SocketUdp::bindSocket( const std::string& address_ip, int port_value, unsigned long timeout ) {
	if ( !setAddressPort(address_ip, port_value) ) {
		LOG(LOG_CRITICAL, ":bindSocket(%s:%d, %lu) FAILED Setting address", address_ip.c_str(), port_value, timeout );
		return WRET_ERR_PARAM;
	 }
	return bindSocket(timeout);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool SocketUdp::setAddressPort( const std::string& add_value, int port_value ) {
	if ( isBinded() ) {
		LOG(LOG_CRITICAL, ":setAddressPort(%s, %d) Not permitted while Binded!", add_value.c_str(), port_value );
		return false;
	 }
	this->address = add_value;
	this->port = port_value;
	this->address_port = Utilities::format("%s:%d", this->address.c_str(), this->port);
	LOG(LOG_INFO, ":setAddressPort() set to '%s'", this->address_port.c_str() );
	return true;
 }

bool SocketUdp::setAddressPort( const std::string& value ) {
	if ( isBinded() ) {
		LOG(LOG_CRITICAL, ":setAddressPort(%s) Not permitted while Binded!", value.c_str() );
		return false;
	 }
	this->address = wosh::network::NetworkUtilities::getAddressFromDestination(value);
	this->port = wosh::network::NetworkUtilities::getPortFromDestination(value);
	this->address_port = value;
	LOG(LOG_INFO, ":setAddressPort() set to '%s'", this->address_port.c_str() );
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace network
}; // namespace wosh
